package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole(Role role) {

        List<Role> allRole = roleMapper.findAllRole(role);

        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleid) {
        List<Integer> allIds = roleMapper.findMenuByRoleId(roleid);
        return allIds;
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        //1. 清空中间表的关联关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //2. 为角色分配菜单

        for (Integer mid : roleMenuVo.getMenuIdList()) {

            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            //封装数据
            Date date = new Date();
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);

            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedby("system");


            roleMapper.roleContextMenu(role_menu_relation);
        }

    }

    @Override
    public void deleteRole(Integer roleid) {


        //调用根据roleid清空中间表关联关系
        roleMapper.deleteRoleContextMenu(roleid);

        roleMapper.deleteRole(roleid);
    }

    @Override
    public ResourceCategory findResourceCategoryAndResourcesByRoleId(Integer roleId) {
        //调用mapper接口查询出资源列表
        List<Resource> resourceList = roleMapper.findResourcesByRoleId(roleId);
        ResourceCategory categoryByRoleId = roleMapper.findResourceCategoryByRoleId(roleId);

        //封装数据
        ResourceCategory resourceCategory = new ResourceCategory();
        resourceCategory.setId(categoryByRoleId.getId());
        resourceCategory.setSort(categoryByRoleId.getSort());
        resourceCategory.setCreatedTime(new Date());
        resourceCategory.setUpdatedTime(new Date());
        resourceCategory.setCreatedBy("admin");
        resourceCategory.setUpdatedBy("admin");
        resourceCategory.setResourceList(resourceList);

        return resourceCategory;
    }

    @Override
    public void RoleContextResource(RoleResourceVo roleResourceVo) {
        //1、根据角色id清空中间表关联
        roleMapper.deleteRoleContextResource(roleResourceVo.getRoleId());

        //2、再重新建立关系
        for(Integer resourceId : roleResourceVo.getResourceIdList()){
            //封装数据
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setRoleId(roleResourceVo.getRoleId());
            roleResourceRelation.setResourceId(resourceId);

            Date date = new Date();
            roleResourceRelation.setCreatedTime(date);
            roleResourceRelation.setUpdatedTime(date);

            roleResourceRelation.setCreatedBy("admin");
            roleResourceRelation.setUpdatedBy("admin");
        }
    }

}

